<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="投票标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入投票标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      
      <el-form-item label="投票开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择投票开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="投票结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择投票结束时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="pollsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="投票标题" align="center" prop="title" />
      <el-table-column label="投票描述" align="center" prop="description" />
      
      <el-table-column label="投票开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投票结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      

      <el-table-column prop="status" label="审批状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hdh_status" :value="scope.row.status"/>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
   
        <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hdh:polls:edit']"       
          >投票</el-button>
          
        </template>
      </el-table-column>
    </el-table>
    

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 是否匿名选择对话框 -->
<el-dialog
  
  :visible.sync="isAnonymousDialogVisible"
  width="300px"
  append-to-body
>
  <!-- 字体和单选按钮居中 -->
  <div style="text-align: center; margin-top: 10px;">
    <el-radio-group v-model="tempAnonymous">
      <el-radio
        v-for="dict in dict.type.zxh_isanonymous"
        :key="dict.value"
        :label="dict.value"
      >
        {{ dict.label }}
      </el-radio>
    </el-radio-group>
  </div>

  <!-- 按钮居中 -->
  <div slot="footer" class="dialog-footer" style="text-align: center;">
    <el-button type="primary" @click="confirmAnonymous">确 定</el-button>
    <el-button @click="cancelAnonymousDialog">取 消</el-button>
  </div>
</el-dialog>

    <!-- 添加或修改投票事项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="投票标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入投票标题" readonly />
        </el-form-item>
        <el-form-item label="投票描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入投票描述" readonly />
        </el-form-item>
        <el-form-item label="选项">
          <el-select v-model="form.optionId" placeholder="请选择选项">
            <el-option
              v-for="item in optionOptions"
              :key="item.optionId"
              :label="item.optionText"
              :value="item.optionId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否匿名">
          <el-radio-group v-model="form.isAnonymous" disabled>
            <el-radio
              v-for="dict in dict.type.zxh_isanonymous"
              :key="dict.value"
              :label="dict.value"
            >
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-input v-model="form.startTime" readonly />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-input v-model="form.endTime" readonly />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listpollsvoted,listPolls, getPolls,votePolls } from "@/api/hdh/polls";
import { getAllOptions,getOptions,listOptions } from "@/api/hdh/options";
export default {
  name: "Polls",
   dicts: ['zxh_isanonymous','hdh_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票事项表格数据
      pollsList: [],
      optionOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isAnonymousDialogVisible: false, // 是否匿名选择对话框状态
      tempAnonymous: null, // 临时存储是否匿名的选择
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        creator: null,
        title: null,
        description: null,
        isAnonymous: null,
        startTime: null,
        endTime: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
        flowInstantId: null,
        status: null
      },
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        pollId: null,
        optionText: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询投票事项列表 */
    getList() {
      this.loading = true;
      listpollsvoted(this.queryParams).then(response => {
        this.pollsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pollId: null,
        creator: null,
        title: null,
        description: null,
        isAnonymous: null,
        startTime: null,
        endTime: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
        flowInstantId: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pollId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    
    // 修改按钮：先弹出匿名选择框
    handleUpdate(row) {
      this.reset();
      this.tempAnonymous = null;
      this.isAnonymousDialogVisible = true; // 显示是否匿名选择对话框
      this.pendingRow = row; // 保存行数据，等确认匿名后使用
    },

    // 确认匿名选择后加载数据并显示表单
    confirmAnonymous() {
      if (this.tempAnonymous === null) {
        this.$message.warning("请选择是否匿名");
        return;
      }
      const row = this.pendingRow;
      const pollId = row.pollId || this.ids;
      this.form.isAnonymous = this.tempAnonymous; // 设置是否匿名
      getPolls(pollId).then(response => {
        this.form = { ...response.data, isAnonymous: this.tempAnonymous };
        this.open = true; // 打开表单对话框
        this.title = "投票";
      });
      this.queryParams1.pollId = pollId;
      listOptions(this.queryParams1).then(response => {
        this.optionOptions = response.rows;
      });
      this.isAnonymousDialogVisible = false; // 关闭匿名选择对话框
    },

    // 取消匿名选择
    cancelAnonymousDialog() {
      this.isAnonymousDialogVisible = false;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pollId != null) {
            votePolls(this.form).then(response => {
              this.$modal.msgSuccess("投票成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPolls(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pollIds = row.pollId || this.ids;
      this.$modal.confirm('是否确认删除投票事项编号为"' + pollIds + '"的数据项？').then(function() {
        return delPolls(pollIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hdh/polls/export', {
        ...this.queryParams
      }, `polls_${new Date().getTime()}.xlsx`)
    },


    

  }
};
</script>
